package com.ahao.sdk.ui.base;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.ahao.abstraction.ISDKListener;
import com.ahao.abstraction.LoginResult;
import com.ahao.abstraction.PayParams;
import com.ahao.sdk.AHAOPlatformSDKManager;
import com.ahao.sdk.utils.JsonUtils;
import com.ahao.sdk.utils.SharedPreferencesUtils;

import org.json.JSONObject;

import java.util.HashMap;

public class JSBridge {
    private final Dialog mDialog;

    public JSBridge(Dialog dialog) {
        mDialog = dialog;
    }

    @JavascriptInterface
    public ISDKListener getSDKListener() {
        return new ISDKListener() {
            @Override
            public void onLoginResult(int code, Object result) {
                AHAOPlatformSDKManager.getInstance().getSDKListener().onLoginResult(code, result);
            }

            @JavascriptInterface
            public void onLoginJsonStrResult(int code, String jsonStr) {
                try {
                    if (code == 0) {
                        LoginResult loginResult = new LoginResult();
                        JSONObject jsonObject = new JSONObject(jsonStr);
                        loginResult.setUserId(jsonObject.getString("uid"));
                        loginResult.setToken(jsonObject.getString("token"));
                        loginResult.setUsername(jsonObject.getString("username"));
                        this.onLoginResult(code, loginResult);
                    } else {
                        this.onLoginResult(code, jsonStr);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @JavascriptInterface
            @Override
            public void onInitResult(int code, String msg) {
                AHAOPlatformSDKManager.getInstance().getSDKListener().onInitResult(code, msg);
            }

            @JavascriptInterface
            @Override
            public void onPayResult(int code, String msg) {
                AHAOPlatformSDKManager.getInstance().getSDKListener().onPayResult(code, msg);

            }

            @Override
            public void onCustomEvent(int type, HashMap data) {
                AHAOPlatformSDKManager.getInstance().getSDKListener().onCustomEvent(type, data);

            }

            @Override
            public void onLogoutResult(int code, Object result) {
                AHAOPlatformSDKManager.getInstance().getSDKListener().onLogoutResult(code, result);

            }
        };
    }

    @JavascriptInterface
    public void close() {
        mDialog.cancel();
    }

    @JavascriptInterface
    public void openBrowser(String url) {
        try {
            Context context = mDialog.getContext();
            new Intent();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @JavascriptInterface
    public void emit(String type, String data) {
        AHAOPlatformSDKManager.getInstance().emit(mDialog.getContext(), type, data);
    }

    @JavascriptInterface
    public String getPayParams() {
        return JsonUtils.toJson(AHAOPlatformSDKManager.getInstance().getPayParams());
    }

    @JavascriptInterface
    public void showToast(String content) {
        Toast.makeText(mDialog.getContext(),content, Toast.LENGTH_SHORT).show();
    }

    @JavascriptInterface
    public String getMetaDataValue (String metaName){
        try {
            return String.valueOf(mDialog.getContext().getPackageManager().getApplicationInfo(mDialog.getContext().getPackageName(), 128).metaData.get(metaName));
        } catch (PackageManager.NameNotFoundException e) {
            return "";
        }
    }

    @JavascriptInterface
    public String getToken(){
        // 获取token
        return SharedPreferencesUtils.getToken(mDialog.getContext());
    }

    @JavascriptInterface
    public void setToken(String string){
        // 设置token
        SharedPreferencesUtils.setToken(mDialog.getContext(), string);
    }

    @JavascriptInterface
    public String getSharedPreferencesByName(String name){
        return SharedPreferencesUtils.getSharedPreferencesString(mDialog.getContext(), name);
    }

    @JavascriptInterface
    public void saveSharedPreferencesByName(String name, String value){
        SharedPreferencesUtils.setSharedPreferencesString(mDialog.getContext(), name, value);
    }

}
